package com.gainloop.Adapters;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gainloop.R;
import com.gainloop.Api.ApiResponseObject;
import com.gainloop.Api.Users;
import com.gainloop.Api.Entity.Help;
import com.gainloop.Api.Entity.User;
import com.gainloop.Data.Registry;
import com.gainloop.Listeners.HelpDescButtonListener;
import com.gainloop.Listeners.HelpIgnoreButtonListener;
import com.gainloop.Listeners.HelpResolvedButtonListener;
import com.gainloop.Listeners.HelpUserButtonListener;
import com.gainloop.Utils.ImageThreadLoader;
import com.gainloop.Utils.ImageThreadLoader.ImageLoadedListener;

public class HelpAdapter extends ArrayAdapter<Help> {

	private ArrayList<Help> items;
	private boolean ownerWish=false;
	
	public HelpAdapter(Context context, int textViewResourceId,
			ArrayList<Help> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.help_item, null);
		}
		Help item = items.get(position);
		User user=getUserById(item);
		if (item != null) {
			final ImageView profile = (ImageView) v
					.findViewById(R.id.wishItemProfileImg);
			TextView content = (TextView) v
					.findViewById(R.id.wishItemContent), username = (TextView) v
					.findViewById(R.id.wishItemUsername),ignoreButton=(TextView) v
					.findViewById(R.id.helpItemIgnore),resolvedButton=(TextView) v
					.findViewById(R.id.helpItemResolved);

			try {
//				URL newurl = new URL(
//						"http://graph.facebook.com/" + user.getFbId() + "/picture?type=square");
//				Bitmap bitmap = BitmapFactory.decodeStream(newurl
//						.openConnection().getInputStream());
//				profile.setImageBitmap(bitmap);
				Bitmap cachedImage = ImageThreadLoader.GetInstance().loadImage("http://graph.facebook.com/" + user.getFbId() + "/picture?type=square", 
						new ImageLoadedListener() {
					public void imageLoaded(Bitmap imageBitmap) {
						profile.setImageBitmap(imageBitmap);
						notifyDataSetChanged();                
					}
				});
				if(cachedImage != null)
					profile.setImageBitmap(cachedImage);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			username.setText(""+user.getName());
			if (!ownerWish){
				ignoreButton.setVisibility(View.INVISIBLE);
				resolvedButton.setVisibility(View.INVISIBLE);
			}else{
				ignoreButton.setOnClickListener(new HelpIgnoreButtonListener(this.getContext(),items,this,item));
				resolvedButton.setOnClickListener
				(new HelpResolvedButtonListener(this.getContext(),items,this,item));
			}
			username.setOnClickListener(new HelpUserButtonListener(user,this));
				
				
			content.setText(item.getContent());
			content.setOnClickListener(new HelpDescButtonListener(item.getWishId(),this));
			//category.setText(item.getCategory());
			//helps.setText("" + item.getHelpsNum());
		}
		return v;
	}
	public void setOwnerWish(){
		ownerWish=true;
	}
	private User getUserById(Help item){
		Users users=new Users();
		User user;
		try {
			ApiResponseObject<User> resp=users.getById(item.getUserId());
			if (resp.getReturnCode()!=0){
				throw new RuntimeException(resp.getError()+item.getUserId());
			}
			else{
				user=resp.getData();
				return user;
			}
			
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
}